//
//  NoticeListViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-8-10.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "NoticeListViewController.h"
#import "Notice.h"
#import "NoticeCell.h"
#import "ServerInterface.h"
#import "Contact.h"
#import "NoticeDetailViewController.h"
#import "MJRefresh.h"
extern Contact *globalContact;
@interface NoticeListViewController ()

@end

@implementation NoticeListViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

    [self initData];
    
    
    __weak NoticeCategory * noticeCategory = category;
    __weak NoticeListViewController * controller = self;
    [self.tableView addHeaderWithCallback:^{
        NSDictionary *categoryDic = @{@"company_id":CompanyID,@"token":globalContact.token,@"user_id":@(globalContact.userID),@"category":@(noticeCategory.ID)};
        [ServerInterface getCategoryNoticesWithDic:categoryDic withBlock:^(NSDictionary *newDic, NSError *error) {
           // NSLog(@"news is:%@",newDic);
            if (error) {
                //NSLog(@"error is:%@",error);
            }
            else{
                if ([[newDic valueForKeyPath:@"success"] boolValue]) {
                    NSArray *newArr = newDic[@"data"];
                    NSArray *arr = [controller analsyisNotices:newArr];
                    [Notice saveWithNotices:arr];
                    [controller initData];
                    [controller.tableView reloadData];
                    
                }
                else{
                }
            }
            [controller.tableView headerEndRefreshing];
        }];
    }];
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableView.headerRefreshingText = @"正在更新最新消息";
    [self.tableView headerBeginRefreshing];

    

}
-(void)viewWillAppear:(BOOL)animated{
    self.title =  category.name;
}


-(void)initData{
     notices = [Notice getNoticesWithCategoryID:category.ID];
}

-(NSArray *)analsyisNotices:(NSArray *)_notices{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:_notices.count];
    for (NSDictionary *noticeDic in _notices) {
        Notice *notice = [Notice analysisNoticeWithDic:noticeDic];
        notice.categoryID = category.ID;
        [arr addObject:notice];
    }
    return arr;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initWithCategory:(NoticeCategory *)_category{
    category = _category;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return notices.count;
}


- (NoticeCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoticeCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[NoticeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NoticeCell"];
    }
    Notice *notice = notices[indexPath.row];
    [cell initWithNotice:notice];

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
     NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:selected animated:YES];
    NoticeDetailViewController *detailView = segue.destinationViewController;
    Notice *notice = notices[selected.row];
    [detailView initNotice:notice];
    detailView.finishLoadBlock = ^(){
        [self.tableView reloadRowsAtIndexPaths:@[selected] withRowAnimation:UITableViewRowAnimationNone];
    };
}


@end
